<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\facade\Session;
class Clock extends controller
{
   public function index()
   {
		return $this->fetch('index');
   }
   public function clock_rules(){
   		return $this->fetch('clock_rules');
   }
   public function challenge(){
   	$res=Db::table('tb_clockin_log')->find();
      $money=$res['money'];
      $status=$res['status'];
      $uid=$res['uid'];
      Session::set('uid',$uid);
      $dang=time();
      $begiY=mktime(8,0,0,date('m'),date('d'),date('Y'));//今天的八点
      $endY=mktime(6,0,0,date('m'),date('d')+1,date('Y'));//第二天的六点
      if($dang>=$begiY || $dang<=$endY){
         if($status==0){
            if($money>=1){
               $data=Db::table('tb_clockin_log')->where('uid',$uid)->update(['status'=>1,'money'=>$money-1]);
               if($data){
                   echo json_encode(['code'=>'success','msg'=>'挑战打卡成功']);die;
               }
            }else{
               echo json_encode(['code'=>'error','msg'=>'对不起您的余额不足']);die;
            }
         }else{
            echo json_encode(['code'=>'error','msg'=>'对不起您已经参加过打卡报名']);die;
         }
      }else{
         echo json_encode(['code'=>'error','msg'=>'对不起，打卡报名时间已经结束']);die;
      }
   }
   public function clock(){
      $res=Db::table('tb_clockin_log')->find();
      $status=$res['status'];
      $uid=$res['uid'];
      $bonus=(50/24)*2;//(剩余金额/剩余红包数)*2
      if($status==1){
         $res2=Db::table('tb_clockin_log')->where('uid',$uid)->update(['status'=>2,'bonus'=>$bonus]);
         if($res2){
            echo json_encode(['code'=>'success','msg'=>'打卡任务已完成，并且分配到金额']);die;
         }
      }else{
         echo json_encode(['code'=>'error','msg'=>'请您先进行报名']);die;
      }
   }
}

// if($status==2){
            
//          }else{
//             echo json_encode(['code'=>'error','msg'=>'请您先打卡哦']);die;
//          }
